<template>
	<view class="container">
		<view class="content">
			<view class="search">
				<uv-search placeholder="请输入订单号" showAction="true" v-model="query.merOrderNo"
					@custom="changeSearch"></uv-search>

			</view>
			<uv-drop-down ref="dropDown" sign="dropDown_1" text-active-color="#3c9cff"
				:extra-icon="{name:'arrow-down-fill',color:'#666',size:'26rpx'}"
				:extra-active-icon="{name:'arrow-up-fill',color:'#3c9cff',size:'26rpx'}" :defaultValue="defaultValue"
				:custom-style="{}" @click="selectMenu">
				<uv-drop-down-item name="order" type="2" :label="dropItem('order').label"
					:value="dropItem('order').value">
				</uv-drop-down-item>
				<uv-drop-down-item name="type" type="2" :label="dropItem('type').label" :value="dropItem('type').value">
				</uv-drop-down-item>
				<uv-drop-down-item name="orderTypeArr" type="2" :label="dropItem('orderTypeArr').label"
					:value="dropItem('orderTypeArr').value">
				</uv-drop-down-item>
				<uv-drop-down-item name="vip_type" type="1" label='时间选择' :value="dropItem('vip_type').value"
					@maskClick="maskClick">
				</uv-drop-down-item>
				<uv-calendars ref="calendars" mode="range" @confirm="confirm" />
			</uv-drop-down>
			<uv-drop-down-popup sign="dropDown_1" :click-overlay-on-close="true" :currentDropItem="currentDropItem"
				@clickItem="clickItem" @popupChange="change"></uv-drop-down-popup>
		</view>
		<scroll-view scroll-y="true" class="scrollbox" @scrolltolower="lower" enable-back-to-top="true"
			@scrolltoupper="upper" lower-threshold="112">
			<view class="merchantList">
				<view class="ArrList">
					<view class="List" v-for="(item,index) in fenecList" :key="index">
						<view class="merchan-data">
							<view class="imgData">
								<img style="max-width: 100rpx;max-height: 100rpx;" src="../../static/yinlian.png" alt=""
									v-if="item.payType == '1'" />
								<img style="max-width: 100rpx;max-height: 100rpx;" src="../../static/zhifubaozhifu.png"
									alt="" v-if="item.payType == '2'" />
								<img style="max-width: 100rpx;max-height: 100rpx;" src="../../static/weixinzhifu.png"
									alt="" v-if="item.payType == '3'" />
								<img style="max-width: 100rpx;max-height: 100rpx;" src="../../static/transaction.png"
									alt="" v-if="item.payType == '4'" />
								<img style="max-width: 100rpx;max-height: 100rpx;" src="../../static/transaction.png"
									alt="" v-if="item.payType == '5'" />
								<img style="max-width: 100rpx;max-height: 100rpx;" src="../../static/transaction.png"
									alt="" v-if="item.payType == '6'" />
								<img style="max-width: 100rpx;max-height: 100rpx;" src="../../static/transaction.png"
									alt="" v-if="item.payType == '0'" />
							</view>
							<view class="text">
								<title style="margin-top: 15rpx;font-size: 25rpx;color: #242f66;">
									交易金额：{{item.txnAmt || ""}}元
								</title>
								<view style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">
									<title style="margin-top: 2rpx;font-size: 22rpx;color: #242f66;">
										订单号：
										<title style="font-size:18rpx">
											{{item.merOrderNo || ""}}
										</title>
									</title>
								</view>
								<title style="margin-top: 2rpx;font-size: 22rpx;color: #242f66;">交易类型：
									<text v-if="item.orderType == 'PAY'">消费</text>
									<text v-if="item.orderType == 'REFUND'">退款</text>
								</title>
								<title style="margin-top: 2rpx;font-size: 22rpx;color: #242f66;">支付类型：
									<text v-if="item.payType == '1'">银行卡</text>
									<text v-if="item.payType == '2'">支付宝</text>
									<text v-if="item.payType == '3'">微信</text>
									<text v-if="item.payType == '4'">银联二维码</text>
									<text v-if="item.payType == '5'">数币</text>
									<text v-if="item.payType == '6'">现金</text>
									<text v-if="item.payType == '0'">未知</text>
								</title>
								<title style="margin-top: 2rpx;font-size: 22rpx;color: #242f66;">交易时间：<title
										style="font-size: 18rpx;color: #242f66;">{{item.txnTime || ""}}
									</title>
								</title>
								<title style="margin-top: 2rpx;font-size: 22rpx;color: #242f66;">交易状态：
									<text style="color: #437c43;" v-if="item.status == 'S'">交易成功</text>
									<text style="color: #ff5353;" v-if="item.status == 'E'">交易失败</text>
									<text style="color: #e4cc68;" v-if="item.status == 'T'">处理中</text>
									<text style="color: #ffbf68;" v-if="item.status == 'F'">退款申请</text>
									<text style="color: #626eff;" v-if="item.status == 'C'">已撤销</text>
									<text style="" v-if="item.status == 'N'">未知状态</text>
								</title>
							</view>

							<view class="" style="margin: 80rpx 30rpx 30rpx 30rpx;">
								<view class="button-wrap1">
									<uv-button type="primary" size="small" text="详情"
										@click="detailsPage(item)"></uv-button>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<tabBar></tabBar>
	</view>
</template>

<script>
	import {
		order
	} from "../../network/transaction.js"
	export default {
		computed: {
			dropItem(name) {
				return (name) => {
					const result = {};
					const find = this.result.find(item => item.name === name);
					if (find) {
						result.label = find.label;
						result.value = find.value;
					} else {
						result.label = this[name].label;
						result.value = this[name].value;
					}
					return result;
				}
			},
			// 获取当前下拉筛选项
			currentDropItem() {
				return this[this.activeName];
			}
		},
		data() {
			return {
				query: {
					merNo: "",
					status: "",
					orderType: "",
					startTime: "",
					endTime: "",
					orderType: "",
				},
				params: {
					page: 1,
					pageSize: 10,
				},
				fenecList: [],
				value: Number(new Date()),
				value1: Number(new Date()),
				// 表示value等于这些值，就属于默认值
				defaultValue: [0, 'all', '0'],
				// 筛选结果
				result: [{
					name: '',
					label: '',
					value: ''
				}],
				activeName: 'order',
				total: 0,
				order: {
					label: '交易状态',
					value: 'all',
					activeIndex: 0,
					color: '#333',
					activeColor: '#2878ff',
					child: [{
							label: '处理中',
							value: 'T'
						}, {
							label: ' 成功',
							value: 'S'
						}, {
							label: '失败',
							value: 'E'
						},
						{
							label: '已撤销',
							value: 'C'
						},
						{
							label: '退款申请',
							value: 'F'
						},
						{
							label: '未知状态',
							value: 'N'
						}
					]
				},
				type: {
					label: '支付类型',
					value: 'all',
					activeIndex: 0,
					color: '#333',
					activeColor: '#2878ff',
					child: [{
							label: '银行卡',
							value: '1'
						}, {
							label: '支付宝',
							value: '2'
						}, {
							label: '微信',
							value: '3'
						}, {
							label: '银联二维码',
							value: '4'
						},
						{
							label: '数币',
							value: '5'
						},
						{
							label: '现金',
							value: '6'
						},
						{
							label: '未知',
							value: '0'
						}
					]
				},
				orderTypeArr: {
					label: '交易类型',
					value: 'all',
					activeIndex: 0,
					color: '#333',
					activeColor: '#2878ff',
					child: [{
						label: '消费',
						value: 'PAY'
					}, {
						label: '退款',
						value: 'REFUND'
					}]
				},
				vip_type: {
					label: '日期选择',
					value: 0,
					activeIndex: 0
				}

			}
		},
		onLoad(e) {
			const arr = uni.getStorageSync('listData')
			console.log(arr)
			this.query.merNo = arr.merNo
			this.merchantList()
		},
		methods: {
			onPullDownRefresh1() {
				this.params.page = 1;
				this.merchantList()
			},
			onReachBottom1() {
				if (this.params.page * this.params.pageSize < this.total) {
					this.params.page = this.params.page + 1
					this.merchantList()
				}
			},
			merchantList() {
				order(this.params, this.query).then(res => {
					console.log(res)
					const code = res.data.code;
					if (code == "000000") {
						if (this.params.page === 1) {
							this.fenecList = res.data.data.rows
						} else {
							this.fenecList = this.fenecList.concat(res.data.data.rows)
						}
						this.total = res.data.data.total
					} else {
						uni.showToast({
							title: res.data.message
						})
					}
				}).catch(err => {
					// console.log(err)
				})
			},
			changeSearch() {
				const arr = uni.getStorageSync('listData')
				this.query = {
						merNo: arr.merNo,
						status: "",
						orderType: "",
						startTime: "",
						endTime: "",
						orderType: "",
					},
					this.merchantList()
			},
			upper() {
				this.params.page = 1;
				this.merchantList()
			},
			lower() {
				if (this.params.page * this.params.pageSize < this.total) {
					this.params.page = this.params.page + 1
					this.merchantList()
				}
			},
			change(e) {
				console.log('弹窗打开状态：', e);
			},
			/**
			 * 点击每个筛选项回调
			 * @param {Object} e { name, active, type } = e
			 */
			selectMenu(e) {
				const {
					name,
					active,
					type
				} = e;
				this.activeName = name;
				// type 等于1 的需要特殊处理：type不等于1可以忽略
				if (type == 1) {
					this.$refs.calendars.open();
				} else {
					const find = this.result.find(item => item.name == this.activeName);
					if (find) {
						const findIndex = this[this.activeName].child.findIndex(item => item.label == find.label && item
							.value == find.value);
						this[this.activeName].activeIndex = findIndex;
					} else {
						this[this.activeName].activeIndex = 0;
					}
				}
			},
			/**
			 * 点击菜单回调处理
			 * @param {Object} item 选中项 { label,value } = e
			 */
			clickItem(e) {
				this.result = []
				// 下面有重新赋值，所以用let
				let {
					label,
					value
				} = e;
				const findIndex = this.result.findIndex(item => item.name == this.activeName);
				if (this.defaultValue.indexOf(value) > -1 && this[this.activeName].label) {
					label = this[this.activeName].label;
				}
				// 已经存在筛选项
				if (findIndex > -1) {
					this.$set(this.result, findIndex, {
						name: this.activeName,
						label,
						value
					})
				} else {
					this.result.push({
						name: this.activeName,
						label,
						value
					});
				}
				this.result = this.result.filter(item => this.defaultValue.indexOf(item.value) == -1);
				console.log(this.result)
				if (this.result[0].name == "order") {
					this.query.status = this.result[0].value
				}
				if (this.result[0].name == "type") {
					this.query.payType = this.result[0].value
				}
				if (this.result[0].name == "orderTypeArr") {
					this.query.orderType = this.result[0].value
				}
				this.merchantList()

			},
			maskClick(e) {
				console.log('maskClick事件:', e);
			},
			confirm(e) {
				console.log('confirm', e);
				this.query.startTime = e.range.before
				this.query.endTime = e.range.after
				this.merchantList()
			},
			detailsPage(item) {
				console.log('事件:', item);
				wx.navigateTo({
					url: '/pages/transaction/detailsPage?item=' + encodeURIComponent(JSON.stringify(item))
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		// height: 100vh;
		background: #FFFFFF;
		display: flex;
		align-items: center;
		/* justify-content: center; */
		flex-direction: column;
	}

	.content {
		width: 90%;
	}

	.search {
		margin-top: 20rpx;
		height: 110rpx;
	}

	/deep/.uv-search {
		height: 100rpx !important;
		background: #EFEFEF !important;
		border-radius: 11px !important;
		border: 1px solid #031A6E;
	}

	.merchantList {
		background: #f4f4f4;
		width: 100%;
	}

	.merchan {
		margin: 5% 0% 0% 5%;
		font-weight: bold;
		color: #242f66;
	}

	.ArrList {
		display: flex;
		align-items: center;
		/* justify-content: center; */
		flex-direction: column;
	}

	.List {
		background: #fff;
		height: 220rpx;
		width: 90%;
		border-radius: 20rpx;
		margin-top: 2%;
	}

	.merchan-data {
		display: flex;
	}

	.imgData {
		width: 100rpx;
		height: 100rpx;
		border-radius: 20rpx;
		// background-image: url('../../static/index.png');
		background-size: 100% 100%;
		margin: 60rpx 35rpx 60rpx 35rpx;
	}

	.image {
		max-width: 100rpx;
		max-height: 100rpx;
	}

	.text {
		width: 100%;
		display: flex;
		flex-flow: column;
		overflow: hidden;
	}

	.merchan-data-1 {
		display: flex;
		justify-content: center;
	}

	/deep/.uv-line {
		width: 90% !important;
	}

	.merchan-data-2 {
		margin-top: 8%;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}

	.scrollbox {
		background: #f4f4f4;
		height: calc(100vh - 380rpx);
	}

	/deep/.uv-sticky {
		margin-left: -5% !important;
	}

	/deep/.uv-drop-down {
		border-bottom: 0px solid #dadbde !important;
	}
</style>